package kr.bos.Framework;

import kr.bos.ControllerBottom;
import kr.bos.R;
import kr.bos.Define.Def;
import kr.bos.Define.Key;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class BaseActivity extends BaseLifecycle{
	
	protected String m_strClsName = Def.DEFAULT_CLASS_NAME;
	
	protected ControllerBottom mControlPannel = null;
	protected Activity mActivity = null;
	
	protected boolean mOptionA = false;
	protected boolean mOptionB = false;
	
	//lifecycle
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Logger.v("called");
		
		Intent itt = getIntent();
		Bundle bun = null;
		if(itt!=null) {
			bun=itt.getExtras();
			if(bun!=null) {
				String[] option = bun.getStringArray(Key.BUNDLE);
				mOptionA = Boolean.parseBoolean(option[0]);
				mOptionB = Boolean.parseBoolean(option[1]);
			}else {
				Logger.e("No bundle(Start option) received");
			}
		}
		
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		
		mActivity = this;
		mControlPannel = ControllerBottom.getInstance(mActivity); 
		
		if(mControlPannel.isVisible()) {
			mControlPannel.setVisible(true);
			findViewById(R.id.control_lin_outer).setVisibility(View.VISIBLE);
			findViewById(R.id.control_ed_txt_1).clearFocus();
		}
		
		showToast(getClass().getSimpleName()+" A["+(mOptionA?"O":"X")+"]"+" B["+(mOptionB?"O":"X")+"]");
	}

	@Override
	protected void onStart() {
		Logger.v("called");
		super.onStart();
	}
	
	@Override
	protected void onRestart() {
		Logger.v("called");
		super.onRestart();
	}
	
	@Override
	protected void onResume() {
		Logger.v("called");
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		Logger.v("called");
		super.onPause();
	}
	
	@Override
	protected void onStop() {
		Logger.v("called");
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		Logger.v("called");
		super.onDestroy();
	}
	
	//utils
	public void showToast(String text) {
		Logger.v("TOAST : "+text);
		Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
	}
	
	/*
	 * 루트뷰를 redirect하여, main.xml에 해당 뷰를 embed한다.
	 * @see kr.bos.Framework.BaseLifecycle#setContentView(int)
	 */
	@Override
	public void setContentView(int layoutResID) {
		View linListLay = getLayoutInflater().inflate(layoutResID, null);
		((LinearLayout)findViewById(R.id.mainbody)).addView(linListLay);
//		super.setContentView(layoutResID);
	}
	
	/* 
	 * 루트뷰를 redirect하여, main.xml에 해당 뷰를 embed한다.
	 * @see kr.bos.Framework.BaseLifecycle#setContentView(android.view.View)
	 */
	@Override
	public void setContentView(View view) {
		((LinearLayout)findViewById(R.id.mainbody)).addView(view);
//		super.setContentView(view);
	}
	
	/// 입력관련

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Logger.v("KEY : "+keyCode+" ACT : "+event.getAction());
		switch(keyCode) {
		case KeyEvent.KEYCODE_MENU:
			mControlPannel.setVisible(!mControlPannel.isVisible());
			break;
		default:
			break;
		}

		return super.onKeyDown(keyCode, event);
	}
	

	@Override
	public void onBackPressed() {
		if(mControlPannel.isVisible()) {
			mControlPannel.setVisible(false);
			return;
		}
		super.onBackPressed();
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if(Logger.USE_LIFECYCLE_LOG || Logger.USE_INPUT_LOG){
			String str = "called";
			if(ev!=null){
				str += printMotionEvent(ev);
			}
			Logger.v(str);
		}
		return super.dispatchTouchEvent(ev);
	}
	
	
	@Override
	public boolean dispatchTrackballEvent(MotionEvent ev) {
		if(Logger.USE_LIFECYCLE_LOG || Logger.USE_INPUT_LOG){
			String str = "called";
			if(ev!=null){
				str += printMotionEvent(ev);
			}
			Logger.v(str);
		}
		return super.dispatchTrackballEvent(ev);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(Logger.USE_LIFECYCLE_LOG || Logger.USE_INPUT_LOG){
			String str = "called";
			if(event!=null){
				str += printMotionEvent(event);
			}
			Logger.v(str);
		}
		return super.onTouchEvent(event);
	}
	
	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		if(Logger.USE_LIFECYCLE_LOG || Logger.USE_INPUT_LOG){
			String str = "called";
			if(event!=null){
				str += printMotionEvent(event);
			}
			Logger.v(str);
		}
		return super.onTrackballEvent(event);
	}

	/// 유틸추가
//
//	@Override
//	public void onClick(View v) {
//		if(Logger.USE_LIFECYCLE_LOG){
//			String str = "called";
//			if(v!=null){
//				str = "called ("+v.getId()+")";
//			}
//			Logger.v(str);
//		}
//	}
//
//	@Override
//	public boolean onTouch(View v, MotionEvent event) {
//		if(Logger.USE_LIFECYCLE_LOG){
//			String str = "called";
//			if(v!=null && event!=null){
//				str = "called ("+v.getId()+")" + "ACT("+event.getAction()+")"+String.format("(%d,%d)", event.getX(), event.getY());
//			}
//			Logger.v(str);
//		}
//		return false;
//	}

	private String printMotionEvent(MotionEvent ev) {
		String str = "";
		try{
			str+=String.format("ACT(%d)POS(%d,%d)",ev.getAction(),(int)ev.getX(),(int)ev.getY());
		}catch(Exception e){
			str+="[ERROR IN PRINT EVENT]";
			e.printStackTrace();
		}
		return str;
	}
}
